//
//  STMarqueeView.swift
//  TTCOM-iOS
//
//  Created by Toj on 1/9/23.
//

import UIKit
import SwiftUI
import TTCOM_iOS

class STMarqueeView: UIView {
    
    func startMarquee() {
        setupMakeLayoutSubviews()
        startTimer()
        timerCallBack()
    }
    
    func stopMarquee() {
        stopTimer()
    }
    
    private var isLandscape: Bool?
    
    @objc private func orientationDidChange(noti:NSNotification) {
        if isHidden { return }
        
        //1.获取 当前设备 实例
        
        guard let _isLandscape = UIDevice.current.orientation.do_isLandscape,  _isLandscape != isLandscape else { return }

        print("-->toj: \(_isLandscape)")
        isLandscape = _isLandscape
        timerCallBack()
    }
    
    @objc private func timerCallBack() {
        guard let superView = superview else { return }
        
        let superWidth  = superView.frame.width
        let superHeight = superView.frame.height
        let selfWidth  = frame.width
        let selfHeight = frame.height
        let widthX  = superWidth  - selfWidth
        let heightY = superHeight - selfHeight
        guard widthX > 0, heightY > 0 else { return }
        
        let randomWidth  = UInt32(widthX)
        let randomHeight = UInt32(heightY)
        let x = arc4random_uniform(randomWidth)
        let y = arc4random_uniform(randomHeight)
        frame.origin.x = CGFloat(x)
        frame.origin.y = CGFloat(y)
    }
    
    private var timer: Timer?
    private func startTimer() {
        let _time = Timer(10, target: self, selector: #selector(timerCallBack))
        timer = _time
    }
    
    private func stopTimer() {
        timer?.invalidate()
        timer = nil
    }
    
    private(set) lazy var marqueeLabel: UILabel = {
        let view = UILabel()
        view.textColor = .white
        view.font = 14.to_boldFont
        return view
    }()
    
    private(set) lazy var marqueeImageView: UIImageView = {
        let view = UIImageView()
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupAddObserver()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print("-->toj: " + "func:" + #function + ", className" + cm_objectName)
        NotificationCenter.default.removeObserver(self)
    }
}

extension STMarqueeView {
    
    private func setupAddObserver() {
        NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange(noti:)), name: UIDevice.orientationDidChangeNotification, object: nil)
    }
    
    private func setupMakeAddSubviews() {
        addSubview(marqueeLabel)
        addSubview(marqueeImageView)
    }
    
    private func setupMakeLayoutSubviews() {
        
        var totalWidth: CGFloat = 0
        var totalHeight: CGFloat = 0
        
        if marqueeLabel.text?.isEmpty == false {
            marqueeLabel.sizeToFit()
            let width = marqueeLabel.frame.size.width
            let height = marqueeLabel.frame.size.height
            totalWidth += width
            totalHeight = height
            marqueeLabel.snp.makeConstraints { make in
                make.right.centerY.equalTo(self)
            }
        }
        
        if marqueeImageView.image != nil {
            let width = marqueeImageView.image?.size.width ?? 0
            let height = marqueeImageView.image?.size.height ?? 0
            totalWidth += width
            totalHeight = height > totalHeight ? height : totalHeight
            marqueeImageView.snp.makeConstraints { make in
                make.left.centerY.equalTo(self)
                make.width.equalTo(width)
                make.height.equalTo(height)
            }
        }
        
        frame = CGRect(x: 0, y: 0, width: totalWidth, height: totalHeight)
    }
}
